<?php


class OrderModel extends BaseModel
{

    var $table  = 'order';	

    var $_page_rows = 20;	
	

    function show($keyword='',$searchtype='',$status='')
    {   
	
	     empty($order)?$order = ' buytime DESC ':'';
		 

         $conditions =' 1 ';

		 if($keyword!='' || ($status!='false' && $status!=''))
		{

			$type=$searchtype==1?'o.orderid':'u.user_name';
			if($keyword!='')$conditions.= ' AND '.$type.' LIKE \'%'.$keyword.'%\'';
			if($status!='false' && $status!='')$conditions.= ' AND o.pay = '.$status.' ';
		}


        $table =  $this->_prefix.'order o left join '.$this->_prefix.'product p on o.productid = p.id left join '.$this->_prefix.'users u on u.user_id=o.userid ';



		 $info['page'] = $this->Page(array('table'=>"{$table}",'conditions' =>"{$conditions}",'url' => 'admin/?app=order&act=index'));		
		 
		 $info['data'] = $this->S_Sql_Exe(array('table'=>"{$table}",'fields'=> ' o.*,p.name,p.nowprice,p.bookprice,u.user_name,u.user_email ','conditions' =>"{$conditions}",'order' =>$order,'limit' =>$info['page']['sql_limit']));
	
		foreach ($info['data'] as $key => $value)
		{
			$info['data'][$key]=$value;
		    if(isset($value['bookprice'])&&$value['bookprice']!=0)
	         {
	               $info['data'][$key]['money'] = $value['productnum']*$value['bookprice'];
	         }
			 else
			{
			       $info['data'][$key]['money']=$value['productnum']*$value['nowprice'];			 
			}

		}

  		 return $info;      
    }


    function allticket($keyword='',$searchtype='',$time='',$used='')
    {   
	
	     empty($order)?$order = ' t.ticketid DESC ':'';
		 

         $conditions =' 1 ';

		if($keyword!='' || $time!='' || $used!='' )
		{
			$type=$searchtype==1?'t.number':'u.user_name';
			if($keyword!='')$conditions.= ' and '.$type.' LIKE \'%'.$keyword.'%\'';
			if($time!='')$conditions.= ' and g.perioddate = \''.$time.'\'';
			if($used!='false')$conditions.= ' and t.status = '.$used.'';
		}

        $table =  $this->_prefix.'ticket t left join '.$this->_prefix.'product p on t.productid = p.id left join '.$this->_prefix.'users u on u.user_id=t.uid left join '.$this->_prefix.'group g on t.groupid=g.id ';

		 $info['page'] = $this->Page(array('table'=>"{$table}",'conditions' =>"{$conditions}",'url' => 'admin/?app=order&act=ticketz'));		
		 
		 $info['data'] = $this->S_Sql_Exe(array('table'=>"{$table}",'fields'=> ' t.*,p.name,u.user_name,g.perioddate,g.name as groupname ','conditions' =>"{$conditions}",'order' =>$order,'limit' =>$info['page']['sql_limit']));
	

  		 return $info;      
    }



	function confirmorder($id)
	{

		 $info = $this->S_Sql_Exe(array('table'=>$this->_prefix.'order o left join '.$this->_prefix.'product p on o.productid = p.id','fields' =>' o.*,p.name,p.nowprice,p.bookprice  ','conditions' =>" o.orderid='{$id}'",'return' =>'one'));
		     
		 return $info;

	}

	function orderget($orderid,$uid='')
	{
		$conditions = " orderid='$orderid' and status = 1 ";
		
		if($uid!='')$conditions.=" and userid = '{$uid}' ";
		
		 $info = $this->S_Sql_Exe(array('table' => $this->_prefix.'order','fields' =>'*','conditions' =>"{$conditions}",'return'=>'one'));

		 return $info;

	}

	function moneyadd($money,$uid)
	{
		$sql="update ".$this->_prefix."users set money = money + $money where user_id = ".$uid;
		return $query = $this->db->query($sql); 
	}

	function moneylog($ary){

		return $this->IU_Sql_Exe(array ('table'=>$this->_prefix.'usermoney','data' => $ary, 'mode' => 'INSERT'));
	}	


	function moneyuser($uid='')
	{
		if($uid=='')$uid=$GLOBALS['SCup']->SCup_user_msg['user_id'];

        $info = $this->S_Sql_Exe(array('table'=>$this->_prefix.'users','fields' =>'money','conditions' =>"user_id = '{$uid}'",'limit' =>'1','return' =>'one'));
		return $info['money'];
	}	

	function moneypay($money,$uid)
	{
			
		$sql="update ".$this->_prefix."users set money = money - $money,totalpay = totalpay + $money where user_id = ".$uid;
		return $query = $this->db->query($sql); 

		
	}


	function ordertype($id,$type,$paytime='',$pay='')
	{
		$ary=array(
			'status'=>$type,
	 	);
		if($paytime!='')$ary['paytime']=time();
		if($pay!='')
		{
			$ary['pay']=1;
			$sql='update '.$this->_prefix.'product p left join '.$this->_prefix.'order o on p.id=o.productid set p.totalnum = p.totalnum + 1 where o.orderid = '.$id;
			$this->db->query($sql);
		}

		$result=$this->IU_Sql_Exe(array ('table' => $this->_prefix.'order','data' => $ary,'mode' => 'UPDATE','conditions' =>"orderid = '{$id}'"));


		return $result;
	}



	function productcheck($id,$city='')
	{	
	       $nowtime = time();
           if($city!='')
	       {
			   
		   	$sql='select g.*,p.* from '.$this->_prefix.'group g left join '.$this->_prefix.'product p on g.id= p.groupid where  g.begintime <= \''.$nowtime.'\' and g.overtime > \''.$nowtime.' \' and p.id = '.intval($id).' and g.city = '.intval($city);
			
		   }else
		   {
		   	$sql='select g.*,p.* from '.$this->_prefix.'group g left join '.$this->_prefix.'product p on g.id= p.groupid where  g.begintime <= \''.$nowtime.'\' and g.overtime > \''.$nowtime.' \' and p.id = '.intval($id);
			
		   }
       	return $this->db->get_one($sql);
		
	}
	
    function paytotal($groupid)
    {   
	   
		 $info = $this->S_Sql_Exe(array('table' => $this->_prefix.'order','fields' =>'count(*) as totalorder,sum(productnum) as payroductnum ','conditions' =>"groupid = '{$groupid}' and pay=1",'return'=>'one'));    
		 return $info;
        
    }

	function addsellertomoney($sellerid,$money)
	{
		
		$sql="update ".$this->_prefix."seller set money = money + $money where id = ".$sellerid;
		return $query = $this->db->query($sql); 


	}
	
	function addsellersucnum($sellerid)
	{
		$sql='update '.$this->_prefix.'seller set successnum = successnum + 1 where id = '.intval($sellerid);
		$query = $this->db->query($sql); 
		return true;
	}
 

	function orderticket($orderid)
	{
		  
		 
			$sql='SELECT g.name as groupname,o.groupid,p.name,p.nowprice,p.bookprice,p.id,o.productid,g.successnum,o.orderid,o.productnum,o.userid as ouid FROM '.$this->_prefix.'order o left join '.$this->_prefix.'product p on o.productid = p.id left join '.$this->_prefix.'group g on o.groupid = g.id where o.orderid = '.$orderid;
	
			$result = $this->db->get_one($sql);



		return $result;
	}
	

	function mailcallpay($orderid)
	{
		  
		 
			$sql='SELECT o.*,u.user_email,u.user_name,p.name,g.perioddate FROM '.$this->_prefix.'order o left join '.$this->_prefix.'product p on o.productid = p.id left join '.$this->_prefix.'users u on u.user_id = o.userid left join '.$this->_prefix.'group g on o.groupid=g.id where o.orderid = '.$orderid;
	
			$result = $this->db->get_one($sql);
		    return $result;
	}
	

	function warnofticket($ticketid)
	{
	  
		 
			$sql='SELECT t.*,u.user_email,u.user_name,p.name,g.perioddate FROM '.$this->_prefix.'ticket t left join '.$this->_prefix.'product p on t.productid = p.id left join '.$this->_prefix.'users u on u.user_id = t.uid left join '.$this->_prefix.'group g on t.groupid=g.id where t.ticketid = '.$ticketid;
	
			$result = $this->db->get_one($sql);
		    return $result;
	}
	

	function orderpaylist($groupid)
	{
		$sql='select o.*,u.user_name,u.user_email,p.nowprice,p.bookprice,g.successnum from '.$this->_prefix.'order o left join '.$this->_prefix.'users u on o.userid= u.user_id left join '.$this->_prefix.'product p on o.productid=p.id left join '.$this->_prefix.'group g on o.groupid=g.id where o.groupid = '.$groupid.' and o.pay = 1';
		$result = $this->db->get_all($sql);
			
		return $result;
	}

	function getallpay()
	{
	   $sql='select * from '.$this->_prefix.'payment order by pay_order asc';
		$result = $this->db->get_all($sql);
			
		return $result;
	}

	function getonepay($id)
	{
	   $sql='select * from '.$this->_prefix.'payment where pay_id = '.$id;
		$result = $this->db->get_one($sql);
			
		return $result;
	}

	function updateonlinepay($id)
	{

	$sql='update '.$this->_prefix.'payment  set  is_online = if(is_online>0, 0, 1) where pay_id = '.$id;

		return $this->db->query($sql);
	}

	function dosetpay($ary,$id)
	{

		return $this->IU_Sql_Exe(array ('table'=>$this->_prefix.'payment','data' => $ary, 'mode' => 'UPDATE','conditions' =>"pay_id = '{$id}'"));
	}	



	function producttypedit($id,$type)
	{
		$ary=array(
			'status' => $type,
		);
		if($type==2)
		{
			$sql='select sellerid,groupid from '.$this->_prefix.'product where id = '.intval($id);
 
			$product=$this->db->get_one($sql);
			$this -> addsellersucnum($product['sellerid']);
            $this->IU_Sql_Exe(array ('table'=>$this->_prefix.'group','data' => $ary,'mode' => 'UPDATE','conditions' =>"id = '".$product['groupid']."'"));
		}
		
		$result=$this->IU_Sql_Exe(array ('table'=>$this->_prefix.'product','data' => $ary,'mode' => 'UPDATE','conditions' =>"id = '{$id}'"));

	}

	function mailCron($ary){
		$keys=$values='';
		foreach($ary as $i => $valuez){
			$a=$i=='addtime'?"":',';
			$keys.='`'.$i.'`'.$a;
			$values.='\''.$valuez.'\''.$a;
		}
		$sql='insert into '.$this->_prefix.'cron ('.$keys.') VALUES ('.$values.')';
		$this->db->query($sql);
	}


	function ticketCreate($userid,$productid,$orderid,$groupid)
	{
		$ticketNumber=$orderid.rand('100','999');
		$ticketPassword=rand('100000','999999');
		$ary=array(
			'uid'=> $userid,
			'groupid'=>$groupid,
			'productid'=>$productid,
			'orderid'=>$orderid,
			'number'=>$ticketNumber,
			'password'=>$ticketPassword,
			'status'=> 1
		);

		return $this->IU_Sql_Exe(array ('table'=>$this->_prefix.'ticket','data' => $ary, 'mode' => 'INSERT'));

	}

    /* 插入数据 */
    function deleteticket($id ='')
    {     
		$id = intval($id);
		return $this->D_Sql_Exe(array ('table'=>$this->_prefix.'ticket','conditions' =>"ticketid = '{$id}' "));
        
    }



    /* 插入数据 */
    function del($id ='')
    {      
		return $this->D_Sql_Exe(array ('conditions' =>"orderid = '{$id}' "));
        
    }



}








?>